package cn.lf.android.lawfirm.undertake.apply;


import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.OrdinaryBaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.distribution.modle.LawCase;
import cn.lf.android.lawfirm.undertake.apply.controler.ApplyUnderListConroller;
import cn.lf.android.lawfirm.undertake.apply.view.ApplyUndertakeAdapter;
import cn.lf.android.lawfirm.undertake.approval.UndertakeActivity;
import cn.lf.android.lawfirm.undertake.approval.controler.UnderListConroller;
import cn.lf.android.lawfirm.undertake.approval.view.UndertakeAdapter;
import cn.lf.android.lawfirm.utils.DebugLog;

import static cn.lf.android.lawfirm.R.id.img_left;

public class ApplyUndertakeSearchListActivity extends OrdinaryBaseActivity
        implements ApplyUnderListConroller.ApplyUnderListCallback {

    private ListView mListView;
    private SmartRefreshLayout refreshLayout;
    private ApplyUndertakeAdapter mUndertakeAdapter;
    private List<LawCase> list = new ArrayList<LawCase>();

    private ApplyUnderListConroller mUnderListConroller;
    private int pageIndex = 1;
    /**
     * 待审批（notYet）、已审批（pass）、已退回（sendBack）和全部（all）（必填默认为待审批）（必填）
     **/
    private String state = "notYet";

    @Override
    public int getChildView() {
        return R.layout.activity_undertake_list;
    }

    @Override
    public void initViewId() {
        initTitle();
        state = getIntent().getStringExtra("state");
        refreshLayout = (SmartRefreshLayout) findId(R.id.refreshLayout);
        mListView = (ListView) findId(R.id.lv_list);
        mUndertakeAdapter = new ApplyUndertakeAdapter(this, list, 0);
        mListView.setAdapter(mUndertakeAdapter);
        mRL_search.setClickable(false);

        mSearch.requestFocus();
        openKeybord();
        setLisenters();
    }

    @Override
    public void refreshLoad() {

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case img_left:
                hintKbTwo();
                finish();
                break;
            default:
                break;
        }
    }


    private void initTitle() {
        mImgLeft.setVisibility(View.VISIBLE);
        mImgLeft.setOnClickListener(this);
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText("搜索");
        mRL_search.setVisibility(View.VISIBLE);
    }

    private void setLisenters() {
        mListView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                hintKbTwo();
                return false;
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(0);//0秒
                if (mSearch.getText().toString().length() > 0) {
                    pageIndex = 1;
                    list.clear();
                    mUnderListConroller.onApplyUnderList(mSearch.getText().toString().trim(), String.valueOf(pageIndex), state);
                }
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
                pageIndex++;
                mUnderListConroller.onApplyUnderList(mSearch.getText().toString().trim(), String.valueOf(pageIndex), state);
            }
        });
//        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent = new Intent(ChangeApprovalSearchListActivity.this, CaseDetailActivity.class);
//                intent.putExtra("caseID", list.get(position).caseID);
//                startActivity(intent);
//            }
//        });
        mSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (null != event) {
                    pageIndex = 1;
                    list.clear();
                    mUnderListConroller.onApplyUnderList(mSearch.getText().toString().trim(), String.valueOf(pageIndex), state);
                }
                return false;
            }
        });
        mSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //这里是联通的，有变化就进行网络请求
                if (mSearch.getText().toString().trim().length() == 0) {
                    mClean.setVisibility(View.GONE);
                    list.clear();
                    mUndertakeAdapter.notifyDataSetChanged();
                    return;
                } else {
                    mClean.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void initData() {
        mUnderListConroller = new ApplyUnderListConroller(this, this);
    }

    @Override
    public void onApplyUnderListuccessed(List<LawCase> beans) {
        DebugLog.e("得到7==" + beans);
        list.addAll(beans);
        mUndertakeAdapter.notifyDataSetChanged();
        if (list.size() == 0) {
            noData2.setVisibility(View.VISIBLE);
        } else {
            noData2.setVisibility(View.GONE);
        }

    }

    @Override
    public void onApplyUnderListFailed() {

    }
}
